{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa5099ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844d61bb",
   "metadata": {},
   "source": [
    "作业：\n",
    "\n",
    "1.分别随机生成六个班的考试成绩，每个班50人，考试共三门：Python、数学、语文。每个班级成绩的shape应当是50x3\n",
    "\n",
    "2.将这六个班的考试成绩垂直叠加得到二维数组score。(即score的每一行是一个同学的成绩)\n",
    "\n",
    "3.生成性别数组sex，水平叠加数组sex和score得到data\n",
    "\n",
    "4.分性别计算各科成绩的统计指标：最小值、最大值、平均分、中位数、标准差\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f5a8ab5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 99,   1, 115],\n",
       "       [ 20, 128, 112],\n",
       "       [ 11,  33, 125],\n",
       "       [139,  63,  65],\n",
       "       [132, 119, 140],\n",
       "       [ 45, 118,  29],\n",
       "       [ 37, 125,   7],\n",
       "       [137,   9, 102],\n",
       "       [133,   1,  94],\n",
       "       [ 38,  39, 134],\n",
       "       [135,  23,  12],\n",
       "       [107,   9,  61],\n",
       "       [ 75,  75, 141],\n",
       "       [  4,  15, 136],\n",
       "       [ 20,  10, 138],\n",
       "       [ 74,  30, 142],\n",
       "       [146, 111,  24],\n",
       "       [ 24,   1,  32],\n",
       "       [ 10,   4, 145],\n",
       "       [ 53,  28,  85],\n",
       "       [ 84, 147, 121],\n",
       "       [ 47,  22,  24],\n",
       "       [ 70, 136,  74],\n",
       "       [ 21,  67,   8],\n",
       "       [107, 102, 149],\n",
       "       [ 55, 110, 104],\n",
       "       [ 27,  99,  24],\n",
       "       [112,   9,  11],\n",
       "       [ 48,  87,  30],\n",
       "       [117, 128, 113],\n",
       "       [ 85, 136, 150],\n",
       "       [105,  34,  43],\n",
       "       [ 95,  78,  66],\n",
       "       [ 27, 147, 110],\n",
       "       [ 87,  92,  52],\n",
       "       [ 90, 100, 127],\n",
       "       [106,  78,  58],\n",
       "       [ 62,  83,  85],\n",
       "       [ 26,  41, 149],\n",
       "       [148, 129, 143],\n",
       "       [ 15, 141,   5],\n",
       "       [ 88, 125, 127],\n",
       "       [ 86, 101, 149],\n",
       "       [ 93,  13,  87],\n",
       "       [  3,  75, 115],\n",
       "       [ 25,  65, 140],\n",
       "       [ 65,  91,   3],\n",
       "       [ 80, 114, 109],\n",
       "       [112,  22, 101],\n",
       "       [106,  54,  68]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class1 = np.random.randint(0,151,size=(50,3))\n",
    "class2 = np.random.randint(0,151,size=(50,3))\n",
    "class3 = np.random.randint(0,151,size=(50,3))\n",
    "class4 = np.random.randint(0,151,size=(50,3))\n",
    "class5 = np.random.randint(0,151,size=(50,3))\n",
    "class6 = np.random.randint(0,151,size=(50,3))\n",
    "class6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d535b250",
   "metadata": {},
   "outputs": [],
   "source": [
    "score= np.vstack((class1,class2,class3,class4,class5,class6))\n",
    "# c = np.vstack((class1,class2,class3,class4,class5,class6)\n",
    "# c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "78f17fda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6339f4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##男用0表示，女用1表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c1d76446",
   "metadata": {},
   "outputs": [],
   "source": [
    "sex = np.random.randint(0,2,size=(300,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "437e2011",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.hstack( (sex, score) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ee070612",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   0,  28,  76],\n",
       "       [  0,   1,  72,  46],\n",
       "       [  0,   1,  75, 112],\n",
       "       ...,\n",
       "       [  1, 149,  40, 108],\n",
       "       [  1, 150,  23, 129],\n",
       "       [  1, 150,  25,  14]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#lexsort默认按最后一行元素有小到大排序, 返回最后一行元素排序后索引所在位置\n",
    "data2 = data[np.lexsort(data[:,::-1].T)]\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "afee1610",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.where()[0]+1\n",
    "# np.where(np.diff(data2[:,0]))\n",
    "#data2是根据第一列排序后的数组，根据第一列的值，将数组一份为2，就要找出从哪个位置开始分\n",
    "data_split=np.split(data2, np.where(np.diff(data2[:,0]))[0]+1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "57d26a38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 1])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩最小值\n",
    "data_split[1][:,1:].min(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6b41d4c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([150, 149, 150])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩最大值\n",
    "data_split[1][:,1:].max(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bf948a2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([69.02564103, 69.36538462, 75.24358974])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩平均分\n",
    "data_split[1][:,1:].mean(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9ad575bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([68. , 63. , 76.5])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩中位数\n",
    "np.median(data_split[1][:,1:],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "30c645e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([45.87398412, 44.11960529, 46.16558433])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩标准差\n",
    "data_split[1][:,1:].std(axis=0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "487d8f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩最小值\n",
    "data_split[0][:,1:].min(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bf30019f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([150, 146, 150])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩最大值\n",
    "data_split[0][:,1:].max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dd302d8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([82.95833333, 69.70138889, 81.45138889])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩平均分\n",
    "data_split[0][:,1:].mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "048b737d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([87., 71., 85.])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩中位数\n",
    "np.median(data_split[0][:,1:],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f94a3cfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([41.34355972, 44.2974604 , 42.52888395])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩标准差\n",
    "data_split[0][:,1:].std(axis=0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f01df14f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
